package cn.lj.controller;

import cn.lj.domain.User;
import cn.lj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView Login( String username,String password , HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user = userService.login(username,password);
        if (user!=null){
            //登录成功，将user对象设置到HttpSession作用范围域中
            session.setAttribute("user",user);
            //登录成功，跳转页面　
            mv.setViewName("main");
        }else {
            //登录失败，设置失败信息，并调转到登录页面
            mv.addObject("login_msg","登录名和密码错误，请重新输入");
            // 登录失败，跳转页面
            mv.setViewName("login");
        }
        return mv;
    }
}
